import java.awt.event.*;
import javax.swing.*;
import java.io.FileWriter;

class Test extends JFrame {

    JTextField  jtf = new JTextField(10);
    JTextArea   jta = new JTextArea(6,40);

    public Test() {

        JPanel p = new JPanel();

        JLabel lb= new JLabel("输入整数：");
        JButton b1 = new JButton("计算");
        b1.addActionListener(new js());
        JButton b2 = new JButton("保存");
        b2.addActionListener(new Abc());
        JButton b3 = new JButton("关闭");
        b3.addActionListener(e -> System.exit(0));
        p.add(lb);
        p.add(jtf);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(jta);
        getContentPane().add(p);
        setTitle("java程序");
        setSize(800,600);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main (String[] args) {

        new Test();
    }
    class js implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String xh = jtf.getText();
            Integer x = Integer.parseInt(xh);
            int y= 0;
            int z= 0;
            int a= 0;
            int b= 0;
            for(int i=1;i<=x;i++) {
                if(i%2!=0) {
                    y=y+i*i;
                    z++;
                }
                else {
                    a=a+i*i;
                    b++;
                }
            }
            jta.append("奇数平方和："+y+"\r\n");
            jta.append("奇数个数："+z+"\r\n");
            jta.append("偶数平方和："+a+"\r\n");
            jta.append("偶数个数："+b+"\r\n");

        }
    }
    class Abc implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            try {
                String save =jta.getText();
                FileWriter fw= new FileWriter("js.txt");
                fw.write(save);
                fw.close();
            }
            catch (Exception ex) {
            }
        }
    }
}


